﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Red_Mosquito
{
    public partial class Form1 : Form
    {
        private iTunesControl control;
        private Mosquito mosquito;
        public Boolean nSync;
        delegate void UpdateCallback(String name, String artist, String album);
        delegate void ChangeDisplayCallback(String text);

        public Form1()
        {
            InitializeComponent();

            // Instantiate iTunes Control
            control = new iTunesControl(this);

            // Instantiate Mosquito
            mosquito = new Mosquito(this);

            // Set nSync
            nSync = sync.Checked;
            
            // Initialize "Now Playing"
            control.update();
        }

        public void nowPlaying(String p_name, String p_artist, String p_album)
        {
            // Display correct play button
            if (control.isPlaying)
                play.Image = global::Red_Mosquito.Properties.Resources.pause;
            else
                play.Image = global::Red_Mosquito.Properties.Resources.play;


            // Ignore beyond here if sync is not enabled
            if (!nSync)
                return;

            // Check if called from itunes controller, delegate if so
            if (track.InvokeRequired)
            {
                UpdateCallback d = new UpdateCallback(nowPlaying);
                track.Invoke(d, new object[] { p_name, p_artist, p_album });
            }
            else
            {
                // Amount of differences for cheap fix to multiple events fired at once
                int diff = 0;

                if (!track.Text.Equals(p_name))
                {
                    track.Text = p_name;
                    diff++;
                }
                if (!artist.Text.Equals(p_artist))
                {
                    artist.Text = p_artist;
                    diff++;
                }
                if (!album.Text.Equals(p_album))
                {
                    album.Text = p_album;
                    diff++;
                }

                // Gather lyrics and display them
                if (control.isPlaying)
                    mosquito.search(p_name, p_artist);
            }
        }

        public void changeDisplay(String text)
        {
            if (display.InvokeRequired)
            {
                ChangeDisplayCallback d = new ChangeDisplayCallback(changeDisplay);
                display.Invoke(d, new object[] {text});
            }
            else
                display.Text = text;
        }
        private void sync_CheckedChanged(object sender, EventArgs e)
        {
            // Assign sync
            nSync = sync.Checked;

            // If synced, call for an update
            if (nSync)
                control.update();
        }

        private void expand_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ;
        }

        private void Form1_Closing(object sender, FormClosingEventArgs e)
        {
            // Disconnect controller
            control.disconnect();
        }


        /************************************************************************
         * Back Button Events
         ************************************************************************/
        private void back_MouseDown(object sender, MouseEventArgs e)
        {
            // Back button pressed
            back.Image = global::Red_Mosquito.Properties.Resources.back_pressed;
        }
        private void back_MouseUp(object sender, MouseEventArgs e)
        {
            // Jump back a song
            control.action(iTunesControl.BACK);

            // Back button unpressed but still hovered
            back.Image = global::Red_Mosquito.Properties.Resources.back;
        }
        /************************************************************************
         ************************************************************************/


        /************************************************************************
         * Play Button Events
         ************************************************************************/
        private void play_MouseDown(object sender, MouseEventArgs e)
        {
            // Play button pressed
            if (control.isPlaying)
                play.Image = global::Red_Mosquito.Properties.Resources.pause_pressed;
            else
                play.Image = global::Red_Mosquito.Properties.Resources.play_pressed;
        }
        private void play_MouseUp(object sender, MouseEventArgs e)
        {
            // Toggle Play/Pause
            control.action(iTunesControl.PLAY_PAUSE);

            // Play/Pause button unpressed but still hovered
            if (control.isPlaying)
                play.Image = global::Red_Mosquito.Properties.Resources.pause;
            else
                play.Image = global::Red_Mosquito.Properties.Resources.play;
        }
        /************************************************************************
         ************************************************************************/


        /************************************************************************
         * Forward Button Events
         ************************************************************************/
        private void forward_MouseDown(object sender, MouseEventArgs e)
        {
            // Forward button pressed
            forward.Image = global::Red_Mosquito.Properties.Resources.forward_pressed;
        }
        private void forward_MouseUp(object sender, MouseEventArgs e)
        {
            // Skip forward a song
            control.action(iTunesControl.FORWARD);

            // Forward button unpressed but still hovered
            forward.Image = global::Red_Mosquito.Properties.Resources.forward;
        }
        /************************************************************************
         ************************************************************************/
    }
}
